Public Partial Class BrowseThreads
    Inherits System.Web.UI.Page

    Private Sub BrowseThreads_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        gvwThreads.PageSize = 25
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim forumID As String = Me.Request.QueryString("ForumID")
            lnkNewThread1.NavigateUrl = _
                String.Format(lnkNewThread1.NavigateUrl, forumID)
            lnkNewThread2.NavigateUrl = lnkNewThread1.NavigateUrl
            gvwThreads.DataSource = KDESolutions.Providers.Forums.ForumService.GetThreads(forumID)
            gvwThreads.DataBind()
            Dim forum As KDESolutions.Providers.Forums.DataTypes.ForumDetails = KDESolutions.Providers.Forums.ForumService.GetForumByID(forumID)
            Me.Title = String.Format(Me.Title, forum.Title)
            'ddlForums.SelectedValue = forumID

            ' if the user is not an admin, editor or moderator, hide the grid's column with 
            ' the commands to delete, close or move a thread
            Dim canEdit As Boolean = (Me.User.Identity.IsAuthenticated And _
               (Me.User.IsInRole("Administrators") Or _
               Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators")))
            gvwThreads.Columns(5).Visible = canEdit
            gvwThreads.Columns(6).Visible = canEdit
            gvwThreads.Columns(7).Visible = canEdit
        End If
    End Sub

    Private Sub gvwThreads_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvwThreads.RowCommand
        Select Case e.CommandName
            Case Is = "Close"
                Dim threadPostID As String = gvwThreads.DataKeys(Convert.ToInt32(e.CommandArgument))(0)
                KDESolutions.Providers.Forums.ForumService.CloseThread(threadPostID)
            Case Is = "Delete"
                Dim threadPostID As String = e.CommandArgument
                KDESolutions.Providers.Forums.ForumService.DeletePost(threadPostID)
        End Select
    End Sub

    Private Sub gvwThreads_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwThreads.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btnClose As ImageButton = CType(e.Row.Cells(6).Controls(0), ImageButton)
            btnClose.OnClientClick = "if (confirm('Are you sure you want to close this thread?') == false) return false;"
            btnClose.ToolTip = "Close this thread"
            Dim btnDelete As ImageButton = CType(e.Row.Cells(7).Controls(1), ImageButton)
            btnDelete.OnClientClick = "if (confirm('Are you sure you want to delete this thread?') == false) return false;"
        End If
    End Sub

    Private Sub gvwThreads_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwThreads.RowDeleting
        'KDESolutions.Providers.Forums.ForumService.DeletePost(e.Keys(0))
    End Sub
End Class